<template>
	<div class="container add_edit">
		<el-tabs v-model="tab" :before-leave="onTabBefore">
			<el-tab-pane :label="`基本`" name="basic">
				<basic v-if="tab" ref="basic"></basic>
			</el-tab-pane>
			<el-tab-pane :label="`详情`" name="detail">
				<detail v-if="tab" ref="detail" @detail="detail"></detail>
			</el-tab-pane>
		</el-tabs>
		<el-form v-loading="formLoading" :model="form" ref="ruleForm" label-width="50px">
			<el-form-item>
				<el-button :loading="addBtnLoading" round icon="el-icon-check" class="confirm-btn"
					@click="submitForm()">提交</el-button>
				<el-button round icon="el-icon-back" class="return-btn" @click="returnPage">返回</el-button>
			</el-form-item>
		</el-form>
	</div>
</template>

<script>
	const __name = 'wmz-entitlement';
	import basic from './basic';
	import detail from './detail.vue';
	export default {
		components: {
			basic,
			detail
		},
		data() {
			return {
				dataId: '',
				formLoading: false,
				tab: 'basic',
				form: {
					title: '',
					content: '',
				},
				addBtnLoading: false
			};
		},
		onLoad(e) {
			if (e.id) {
				this.dataId = e.id
				this.loadData()
			}
		},
		methods: {
			async loadData() {
				this.$func.wmzadmin.call('entitlement/get', {
					id: this.dataId
				}).then(res => {
					if (res.code == 200) {
						this.$refs.basic.setData(res.datas.data);
						this.$refs.detail.setData(res.datas.data); 
					}
				}); 
			},
			onTabBefore(activeName, oldActiveName) {
				/* let flag = true;
			
				if (!this.$refs.basic) return;
			
				if (!this.requestSta) {
					flag = false;
				}
			
				if (this.requestSta) {
					this.$refs.basic.getData((res, valid) => {
						flag = valid;
			
						if (!valid && this.tab != 'basic') {
							flag = true;
							this.tab = 'basic';
						} 
			
						if (flag) {
							uni.setStorage({
								key: this.tabsStorage,
								data: activeName
							});
			
							this.$nextTick(() => {
								this.$api.scrollOnStorage('.el-tabs__content', this.$route.path + '-' +
									activeName);
			
								this.$api.scrollToStorage('.el-tabs__content', this.$route.path + '-' +
									activeName);
			
								this.$refs.useAffix.getAffixScrollBox();
							});
			
							return;
						}
			
						uni.setStorage({
							key: this.tabsStorage,
							data: oldActiveName
						});
					});
				}
			
				return flag; */
			},
			submitForm() {
				this.formLoading = true;

				let goods = {};
				let detail = {};
				this.$refs.basic.getData((res, valid) => {
					if (!valid) {
						this.tab = 'basic';
						this.formLoading = false;
						return;
					}
					goods = res;
					this.$refs.detail.getData(async (res, valid) => {
						if (!valid) {
							this.tab = 'detail';
							this.formLoading = false;
							return;
						}

						detail.desc_mobile = res.content;
						goods.img = res.imgs[0].url;
						goods.imgs = res.imgs;
						if (res.video) {
							if (res.video.poster && res.video.poster.indexOf('blob') != -1) {
								const result = await uniCloud.uploadFile({
									filePath: res.video.poster,
									cloudPath: res.video.name.split('.')[0] + '.png'
								});
								let posterUrl = result.fileID;
								if (posterUrl.indexOf('cloud://') !== -1) {
									const urls = await uniCloud.getTempFileURL({
										fileList: [posterUrl]
									});
									if (urls.fileList && urls.fileList.length > 0) {
										posterUrl = urls.fileList[0].tempFileURL || '';
									}
								}

								res.video.poster = posterUrl;
							}
							if (res.video.url && res.video.url.indexOf('blob') != -1) {
								const result = await uniCloud.uploadFile({
									filePath: res.video.url,
									cloudPath: res.video.name
								});
								let videoUrl = result.fileID;
								if (videoUrl.indexOf('cloud://') !== -1) {
									const urls = await uniCloud.getTempFileURL({
										fileList: [videoUrl]
									});
									if (urls.fileList && urls.fileList.length > 0) {
										videoUrl = urls.fileList[0].tempFileURL || '';
									}
								}

								res.video.url = videoUrl;
							}
						}
						goods.video = res.video;
						for (const key in goods) {
							if (goods.hasOwnProperty(key)) {
								if (typeof goods[key] === 'string') {
									if (/^[0-9]*$/.test(goods[key])) {
										goods[key] = parseInt(goods[key]);
									} else if (/^[0-9]+(.[0-9]{1,3})?$/.test(goods[key])) {
										goods[key] = parseFloat(goods[key]);
									}
								}
							}
						}
						goods.content = detail.desc_mobile.replace(/inline-/gi, '');
						if (!this.dataId) {
							this.$func.wmzadmin.call('entitlement/add', {
								goods
							}).then(res => {
								if (res.code===200) {
									this.$message('新增成功');
									this.formLoading = false;
									uni.navigateBack()
								}  else {
									this.$message.error('新增失败');
								}
							})
						} else {
							this.$func.wmzadmin.call('entitlement/update', {
								id: this.dataId,
								goods
							}).then(res => {
								if (res.code===200) {
									this.$message('修改成功');
									this.formLoading = false;
									uni.navigateBack()
								}  else {
									this.$message.error('修改失败');
								}
							})
						}
					});
				});
			},
			editorChange(res) {
				this.form.content = res;
			},
			returnPage() {
				uni.navigateBack()
			},
		},
		created() {
			// this.loadData();
		}
	};
</script>

<style></style>